var express = require('express');
var router = express.Router();
import { Request, Response } from 'express';
import * as lowcodeData from '../../db/lowcodeData'

router.get('/api/lowcode/moduleList', (req: Request, res: Response) => {
  lowcodeData.queryModuleList().then(data => {
    res.send({
      success: true,
      data,
    })
  }).catch(err => {
    res.status(500).send({
      data: {
        err,
      },
      errorCode: '500',
      errorMessage: '数据库错误：' + JSON.stringify(err),
      success: false,
    });
  })
})
router.get('/api/lowcode/moduleInfo', (req: Request, res: Response) => {
  const { id } = req.query;
  lowcodeData.queryModule(Number(id)).then(data => {
    res.send({
      success: true,
      data,
    })
  }).catch(err => {
    res.status(500).send({
      data: {
        err,
      },
      errorCode: '500',
      errorMessage: '数据库错误：' + JSON.stringify(err),
      success: false,
    });
  })
})


//删除模块
router.get('/api/lowcode/deleteModule', (req: Request, res: Response) => {
  const { id } = req.query;
  lowcodeData.deleteModuleById(Number(id)).then(data => {
    res.send({
      success: true,
      data,
    })
  }).catch(err => {
    res.status(500).send({
      data: {
        err,
      },
      errorCode: '500',
      errorMessage: '数据库错误：' + JSON.stringify(err),
      success: false,
    });
  })
})

router.post('/api/lowcode/updateModuleName', (req: Request, res: Response) => {
  const { id, name } = req.body;
  lowcodeData.updateModuleName(id, name).then(data => {
    res.send({
      success: true,
      data,
    })
  }).catch(err => {
    res.status(500).send({
      data: {
        err,
      },
      errorCode: '500',
      errorMessage: '数据库错误：' + JSON.stringify(err),
      success: false,
    });
  })
})
router.post('/api/lowcode/updateModuleScheam', (req: Request, res: Response) => {
  const { id, packages, schema } = req.body;
  lowcodeData.updateModuleSchema(id, [schema, packages]).then(data => {
    res.send({
      success: true,
      data,
    })
  }).catch(err => {
    res.status(500).send({
      data: {
        err,
      },
      errorCode: '500',
      errorMessage: '数据库错误：' + JSON.stringify(err),
      success: false,
    });
  })
})

router.post('/api/lowcode/addModule', (req: Request, res: Response) => {
  // const { id, name,assets } = req.body;
  lowcodeData.addModule(req.body).then(data => {
    res.send({
      success: true,
      data,
    })
  }).catch(err => {
    res.status(500).send({
      data: {
        err,
      },
      errorCode: '500',
      errorMessage: '数据库错误：' + JSON.stringify(err),
      success: false,
    });
  })
})

router.post('/api/lowcode/moduleRelease', (req: Request, res: Response) => {
  const { id, vesion, remark } = req.body;
  lowcodeData.updateModuleRelease(id, [vesion, remark]).then(data => {
    res.send({
      success: true,
      data,
    })
  }).catch(err => {
    res.status(500).send({
      data: {
        err,
      },
      errorCode: '500',
      errorMessage: '数据库错误：' + JSON.stringify(err),
      success: false,
    });
  })
})


module.exports = router;
